<?php

class cate_model extends MY_Model {

    var $table_name = 'categories';
    var $primary_key = 'id';
    
    public function getCateSelectHtml($id = 0,$symbol='',$class=''){
       $result = $this->getCateSelectHtmlTmp($id, $symbol, $class);       
       return $result;
    }
    public function getCateSelectHtmlTmp($id = 0,$symbol='',$class=''){
        static $theFirst = true;
        $sql = "select cat.id, cat.title,cat.parent_id from categories as cat
                where cat.parent_id = $id";
        $query = $this->db->query($sql);        
        $result = array();
        if ($theFirst){
            $theFirst = false;
            $result['0'] = '[ Chọn ]';
        }
        foreach ($query->result_array() as $item){
            $result[$item['id']] = $symbol.$item['title']; ;
            $child = $this->getCateSelectHtml($item['id'], $symbol."  |_");
            foreach ($child as $key=> $childItem){
                $result[$key] = $childItem;
            }
        }
       return $result;
    }
    
    public function createMenuArray($id=0,$depth=2){
         $sql = "select cat.id, cat.title,cat.parent_id from categories as cat
                where cat.parent_id = $id";
         $query = $this->db->query($sql);  
         $result = array();
         foreach ($query->result_array() as $item){
            $result[] = array(
                'depth' => $depth,
                'id' => $item['id'],
                'title' => $item['title'],
                'link' => 'products/'.$item['id']                
            );
            $child = $this->createMenuArray($item['id'],$depth + 1);
            foreach ($child as $key=> $childItem){
                $result[] = $childItem;
            }
        }
        return $result;
    }
     /*
     * [depth] => 1
            [id] => 72
            [title] => Trang Chủ
            [link] => 
            [lft] => 2
            [rgt] => 3
            [menu_id] => 4
     */
}

?>